package datastructure.array

/**
 * [Dynamic Array](https://www.hackerrank.com/challenges/dynamic-array)
 */
// Complete the dynamicArray function below.
fun dynamicArray(n: Int, queries: Array<Array<Int>>): Array<Int> {
    var lastAnswer = 0
    var index: Int
    val seqList = Array(n) { it -> arrayListOf<Int>() }
    var seq: ArrayList<Int>
    val ans = arrayListOf<Int>()
    for (query in queries) {
        index = query[1].xor(lastAnswer) % n
        seq = seqList[index]
        if (query[0] == 1) {
            seq.add(query[2])
        } else {
            lastAnswer = seq[query[2] % seq.size]
            ans.add(lastAnswer)
        }
    }
    return ans.toTypedArray()
}